<?php

namespace Common\Logic;

/**
 * 预定端订单部分逻辑逻辑
 * @modify trainyao 2016-05-11 创建
 */
class OrderBookLogic extends \Common\Controller\BaseController {
    /**
     * 获取订单详情
     * 方法会抛异常
     * @modify trainyao 2016-05-11   创建
     * @param  integer   $orderRoomId 子订单ID
     * @return array                信息数组 [{id, hotelentity_id, roominfo_id, user=> [{
     *                                         id(orderuser_id), mobile, name, identity
     *                                       }, {}, ...]}
     */
    public function getOrderDetail($orderRoomId , $langSet = null) {
        $orderRoomModel = D('OrderRoom');
        $orderRoom = $orderRoomModel->getInfo($orderRoomId);
        if (empty($orderRoom)) {
            throw new \Exception('403,202', 202);
            return false;
        }
        $orderRoom = $orderRoom[0];
        $orderLogic = A('Common/Order', 'Logic');

        $orderInfoModel = D('OrderInfo');
        $orderInfo = $orderInfoModel->getInfo($orderRoom['orderinfo_id'], false);

        // 补充客栈名,地址
        $hotelDetailModel = D('HotelDetail');
        $hotelDeatil = $hotelDetailModel->getInfoByHotel($orderInfo['hotelentity_id']);
        $orderRoom['hotelName'] = $hotelDeatil['name'];
        $orderRoom['address'] = $hotelDeatil['address'];

        // 补充入住人信息
        $orderUserModel = D('OrderUser');
        $orderUserInfo = $orderUserModel->getInfoByOrder($orderInfo['id']);
        $orderRoom['user'] = $orderUserInfo;

        // TODO:补充订单来源信息(暂时应该不需要)

        if ($orderRoom['status'] == ORDER_BOOKED) {
            $orderRoom['orderOutdateTime'] = '--';
        }
        // 补充订单过期时间,从redis取出
        if ($orderRoom['status'] == ORDER_OUTDATED) {
            $redisOrderRecordInfo = $orderRoomModel->getInfoInRedisByHotelRoomOrderRoom($orderRoom['hotelentity_id'], null, $orderRoomId);
            if (!empty($redisOrderRecordInfo)) {
                $orderRoom['status'] = ORDER_BOOKED;
                $orderRoom['orderOutdateTime'] = $redisOrderRecordInfo[0]['orderOutdateTime'];
            }
        }
        $langSet = !is_null($langSet) ? $langSet : LANG_SET;

        // 修改该退房而未退房的子订单状态  status = 6
        $returnData = $orderLogic->modifyOrderRoomStatus([array(
            'hotelentity_id' => $orderRoom['hotelentity_id'],
            'status' => $orderRoom['status'],
            'hour' => $orderRoom['hour'] > 0 ? $orderRoom['hour'] : 0,
            'enddate' => $orderRoom['enddate'],
        )]);
        $orderRoom['status'] = $returnData[0]['status'];

        // 补充订单状态名称
        $orderRoom['statusName'] = $orderLogic->statusMap[$langSet][$orderRoom['status']];
        // 补充订单退款信息
        $orderRoom['refundStatus'] = 0;
        if ($orderRoom['status'] == ORDER_PAYED) {
            $orderRefundModel = D('OrderRefund');
            $orderRefundInfo = $orderRefundModel->getInfoByOrder($orderRoomId);
            if (!empty($orderRefundInfo)) {
                // 若对应两条退款记录,取提前退房的退款状态
                if (count($orderRefundInfo) == 2) {
                    $orderRefundInfo = getArrayValueAsIndex($orderRefundInfo, 'type', 'multi');
                    $orderRoom['refundStatus'] = $orderRefundInfo[REFUND_TYPE_CHECKOUT][0]['status'];
                } else {
                    $orderRoom['refundStatus'] = $orderRefundInfo[0]['status'];
                }
            }
        }

        // 补充房间是否可以获取房卡
        $roomInfoModel = D('RoomInfo');
        $roomInfo = $roomInfoModel->getInfo($orderRoom['roominfo_id']);
        $orderRoom['canGetCard'] = ($roomInfo['roomfrom'] == ROOMFROM_QRM || $roomInfo['roomfrom'] == ROOMFROM_RFANDQRM) ? ENABLE : DISABLE;

        $orderRoom['order'] = $orderInfo;

        return $orderRoom;
    }
}
